<template>
	<view>
		<van-nav-bar title="个人资料设置" left-text="返回" left-arrow @click-left="onClickLeft" />
		<view class="">
			<van-cell title="头像">
				<van-uploader @after-read="userPhoto">
					<img :src="userInfo[0].userPhoto" style="width: 70rpx;height: 70rpx;border-radius:100%;" alt="">
				</van-uploader>
			</van-cell>
			<van-cell title="昵称" @click="AlterName" is-link>
				<view class="">
					{{userInfo[0].userName}}
				</view>
			</van-cell>

			<van-dialog use-slot title="修改昵称" :show="showName" show-cancel-button @confirm="getUserInfo"
				@cancel="onClose">
				<view style="display:flex">
					<view style="margin-left: 30rpx;">
						昵称：
					</view>
					<input v-model="userName" placeholder="请输入昵称">
				</view>
			</van-dialog>

			<van-cell title="手机号">
				<view class="">
					{{userInfo[0].userTel}}
				</view>
			</van-cell>
			<van-cell v-show="userInfo[0].userPwd=='' || userInfo[0].userPwd==null" title="设置登录密码" @click="setPsw" is-link>
			</van-cell>
			
			<van-dialog use-slot title="设置登录密码" :show="showSetPsw" show-cancel-button @confirm="setNewPsw" @cancel="closeNewPsw">
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="NewPwd" type="password" placeholder="请输入新密码">
				</view>
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="NewPwd1" type="password" placeholder="请输入确认密码">
				</view>
			</van-dialog>
			
			<van-cell v-show="userInfo[0].userPwd.length>0" title="修改登录密码" @click="AlterPsw" is-link>
			</van-cell>

			<van-dialog use-slot title="修改登录密码" :show="showPsw" show-cancel-button @confirm="getPsw" @cancel="closePsw">
				<view style="display:flex">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userPsw" type="password" placeholder="请输入原密码">
				</view>
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userNewPsw" type="password" placeholder="请输入新密码">
				</view>
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userNewPsw1" type="password" placeholder="请输入确认密码">
				</view>
			</van-dialog>

			<van-cell title="修改支付密码" @click="AlterPayPsw" is-link>
			</van-cell>

			<van-dialog use-slot title="修改支付密码" :show="showPayPsw" show-cancel-button @confirm="getPayPsw"
				@cancel="closePayPsw">
				<view style="display:flex">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userPayPsw" type="password" placeholder="请输入登录密码">
				</view>
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userPayNewPsw" type="password" placeholder="请输入新支付密码">
				</view>
				<view style="display:flex;margin-top: 30rpx;">
					<view style="margin-left: 30rpx;">
						密码：
					</view>
					<input v-model="userPayNewPsw1" type="password" placeholder="请输入确认支付密码">
				</view>
			</van-dialog>

			<van-cell title="收货地址" @click="ShippingAddress" is-link>
			</van-cell>
			<van-cell v-show="isNo==1" title="已实名认证">
			</van-cell>
			<van-cell v-show="isNo==0" title="实名认证" @click="Certification" is-link>
			</van-cell>

			<van-dialog use-slot title="实名认证" :show="showCertification" show-cancel-button @confirm="getCertification"
				@cancel="closeCertification">
				<view style="display:flex">
					<view style="margin-left: 30rpx;">
						身份证名字：
					</view>
					<input v-model="Name" placeholder="请输入身份证名字">
				</view>
				<view style="display:flex">
					<view style="margin-left: 30rpx;">
						身份证号码：
					</view>
					<input v-model="IdNumber" placeholder="请输入身份证号码">
				</view>
				<view style="margin: 30rpx 180rpx;">
					<van-uploader @after-read="frontPhoto">
						<button>上传正面照片</button>
					</van-uploader>
				</view>
				<view style="margin: 30rpx 180rpx;">
					<van-uploader @after-read="versoPhoto">
						<button>上传反面照片</button>
					</van-uploader>
				</view>

			</van-dialog>

			<van-cell title="注册日期">
				<view class="">
					{{userInfo[0].registrationTime}}
				</view>
			</van-cell>
			<van-cell title="我的信用分">
				<view class="">
					{{userInfo[0].creditScore}}
				</view>
			</van-cell>
		</view>
	</view>
</template>

<script>
	import request from '../../com/request.js'
	export default {
		data() {
			return {
				newPhoto: '',
				userInfo: '',
				showName: false,
				userName: '',
				showPsw: false,
				userPsw: '',
				userNewPsw: '',
				userNewPsw1: '',
				showPayPsw: false,
				userPayPsw: '',
				userPayNewPsw: '',
				userPayNewPsw1: '',
				showCertification: false,
				Name: '',
				IdNumber: '',
				frontImg: '', //身份证正面照
				versoImg: '', //身份证反面照
				showSetPsw:false,
				NewPwd:'',
				NewPwd1:'',
				isNo:0
			}
		},
		methods: {
			onClickLeft() {
				uni.redirectTo({
					url: '/pages/MyCenter/MyCenter'
				});
			},
			userPhoto(res) {
				let that = this
				let tempFilePaths = res.detail.file.thumb
				uni.uploadFile({
					url: 'http://112.74.185.144:8000/api/common/uploadfile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						'file': tempFilePaths
					}
				}).then(resp => {
					that.newPhoto = JSON.parse(resp[1].data).data
					request({
						url: 'api/ChangePhoto/get',
						data: {
							acc: uni.getStorageSync("userTel"),
							photo: that.newPhoto
						},
						success: (resp) => {
							that.selectMyprofile()
						}
					})
				}).catch((err) => {
					console.log(err);
				})
			},
			AlterName() {
				this.showName = true
			},
			getUserInfo() {
				request({
					url: 'api/ChangeName/get',
					data: {
						acc: uni.getStorageSync("userTel"),
						userName: this.userName
					},
					success: (resp) => {
						this.showName = false
						this.selectMyprofile()
					}
				})
			},
			onClose() {
				this.showName = false
				console.log("取消")
			},
			selectMyprofile() {
				request({
					url: 'api/myprofile/get',
					data: {
						acc: uni.getStorageSync("userTel")
					},
					success: (resp) => {
						this.userInfo = resp.data.data
						console.log(this.userInfo, 52);
					}
				})
			},
			AlterPsw() {
				this.showPsw = true
			},
			getPsw() {
				if(this.userNewPsw=='' || this.userNewPsw== null){
					uni.showToast({
						title: '密码不能为空',
					})
				}
				else if(this.userNewPsw!=this.userNewPsw1){
					uni.showToast({
						title: '两次密码输入不一致',
					})
				}else if(this.userPsw!=this.userInfo[0].userPwd){
					uni.showToast({
						title: '原密码错误',
					})
				}else{
					request({
						url: 'api/ChangePwd/get',
						data: {
							acc: uni.getStorageSync("userTel"),
							userNewPwd:this.userNewPsw
						},
						success: (resp) => {
							uni.showToast({
								title: '修改成功',
							})
						}
					})
				}
				this.showPsw = false
			},
			closePsw() {
				this.showPsw = false
			},
			AlterPayPsw() {
				this.showPayPsw = true
			},
			getPayPsw() {
				if(this.userPayNewPsw=='' || this.userPayNewPsw==null){
					uni.showToast({
						title: '密码不能为空',
					})
				}else if(this.userPayNewPsw!=this.userPayNewPsw1){
					uni.showToast({
						title: '两次密码不一致',
					})
				}else if(this.userPayPsw!=this.userInfo[0].userPwd){
					uni.showToast({
						title: '登录密码错误',
					})
				}else{
					request({
						url: 'api/ChangePayPwd/get',
						data: {
							acc: uni.getStorageSync("userTel"),
							newPayPwd:this.userPayNewPsw
						},
						success: (resp) => {
							uni.showToast({
								title: '修改成功',
							})
						}
					})
				}
				this.showPayPsw = false
			},
			closePayPsw() {
				this.showPayPsw = false
			},
			Certification() {
				this.showCertification = true
			},
			getCertification() {
				request({
					url: 'api/confirmCertification/get',
					data: {
						acc: uni.getStorageSync("userTel"),
						identityCardNumber:this.IdNumber,
						identityCardName:this.Name,
						frontImg:this.frontImg,
						versoImg:this.versoImg
					},
					success: (resp) => {
						uni.showToast({
							title: '实名等待审核',
						})
						this.selectCertification()
					}
				})
				this.showCertification = false
			},
			closeCertification() {
				this.showCertification = false
			},
			frontPhoto(res) {
				console.log(res)
				let that = this
				let tempFilePaths = res.detail.file.thumb
				uni.uploadFile({
					url: 'http://112.74.185.144:8000/api/common/uploadfile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						'file': tempFilePaths
					}
				}).then(resp => {
					that.frontImg = JSON.parse(resp[1].data).data
					console.log(that.frontImg,263)
					uni.showToast({
						title: '上传成功',
						icon: "none",
						duration: 2000
					})
				}).catch((err) => {
					console.log(err);
				})
			},
			versoPhoto(res) {
				console.log(res)
				let that = this
				let tempFilePaths = res.detail.file.thumb
				uni.uploadFile({
					url: 'http://112.74.185.144:8000/api/common/uploadfile',
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						'file': tempFilePaths
					}
				}).then(resp => {
					that.versoImg = JSON.parse(resp[1].data).data
					console.log(that.versoImg,282)
					uni.showToast({
						title: '上传成功',
						icon: "none",
						duration: 2000
					})
				}).catch((err) => {
					console.log(err);
				})
			},
			setPsw(){
				this.showSetPsw=true
			},
			setNewPsw(){
				if(this.NewPwd!=this.NewPwd1){
					uni.showToast({
						title: '两次密码输入不一致',
					})
				}else{
					request({
						url: 'api/ChangePwd/get',
						data: {
							acc: uni.getStorageSync("userTel"),
							userNewPwd:this.NewPwd
						},
						success: (resp) => {
							uni.showToast({
								title: '设置成功',
							})
							this.selectMyprofile()
						}
					})
				}
				this.showSetPsw=false
			},
			closeNewPsw(){
				this.showSetPsw=false
				console.log("取消")
			},
			selectCertification(){//查看是否实名
				request({
					url: 'api/selectCertification/get',
					data: {
						acc: uni.getStorageSync("userTel"),
					},
					success: (resp) => {
						console.log(resp,419)
						if(resp.data.data.length>0){
							this.isNo=1
						}
					}
				})
			},
			ShippingAddress(){
				uni.redirectTo({
					url: '/pages/ShippingAddress/ShippingAddress'
				})
			}
		},
		onLoad() {
			this.selectMyprofile()
			this.selectCertification()
		}
	}
</script>

<style>

</style>
